#include "dosVisualize.hpp"
#include "../Algorithm/gameSolver.hpp"
#include <unistd.h>

int main()
{
    string filepath = "./maps/1.map";
    MainProcess mainp(filepath);
    GameSolver solver;

    char movement;
    Navigator navigator;
    string attachText;
    vector<Point2D> empty;
    string pathText;

    cout << "Program start!" << endl;

    // solver.operation.init(mainp.state);
    cout << "totally position is " << solver.operation.posNum << endl;

    /////////////////////
    // 单元测试
    // solver.operation.init(mainp.state);
    // vector<Point2D> box;
    // vector<int> ind = {9, 15, 4};
    // for (int i = 0; i < ind.size(); i++)
    //     box.push_back(solver.operation.reachablePoints[ind[i]]);
    // for (int i = 0; i < box.size(); i++)
    //     cout << box[i].x << ',' << box[i].y << " | ";
    // cout << endl;
    // solver.operation.rankBox(box);
    // for (int i = 0; i < box.size(); i++)
    //     cout << box[i].x << ',' << box[i].y << " | ";
    // return 0;

    // ////////////////////
    // vector<int> boxPositions; // 箱子的位置初始化
    // for (int i = 0; i < mainp.state.target.size(); i++)
    //     boxPositions.push_back(i);
    // int count = 0;
    // while (solver.operation.getNextBoxPosition(boxPositions))
    //     count++;
    // cout << "mostly node is " << count << endl;
    // cin >> movement;
    /////////////////
    /// // 单元测试
    // solver.operation.init(mainp.state);
    // OperationNode opn1, opn2;
    // vector<vector<bool>> tmpMap;
    // vector<bool> empBu;
    // for (int i = 0; i < solver.operation.posNum; i++)
    //     empBu.push_back(false);
    // //////// 1
    // opn1.box = mainp.state.box;
    // opn1.box = {{2, 2}, {3, 1}, {3, 3}, {4, 2}};
    // opn1.code = solver.operation.EncodeBoxPose(opn1.box);

    // opn1.bucket = empBu;
    // for (int i = 0; i < solver.operation.posNum; i++)
    //     for (Point2D b : opn1.box)
    //         if (b == solver.operation.reachablePoints[i])
    //             opn1.bucket[i] = true;

    // solver.operation.initMask(opn1.mask);
    // tmpMap = mainp.state.map;

    // solver.operation.flood(tmpMap, opn1.mask, mainp.state.man);

    
    // solver.operation.printBoxes(opn1.box);
    // solver.operation.printMask(opn1.mask);
    // cout << "is opn1 stack : " << solver.operation.isStacked(opn1) << endl;
    // return 0;
    //////// 2
    // opn2.box = {{2,5}, {2,6},{3,3}, {5, 6}};
    // opn2.code = solver.operation.EncodeBoxPose(opn2.box);

    // opn2.bucket = empBu;
    // for (int i = 0; i < solver.operation.posNum; i++)
    //     for (Point2D b : opn2.box)
    //         if (b == solver.operation.reachablePoints[i])
    //             opn2.bucket[i] = true;

    // solver.operation.initMask(opn2.mask);
    // tmpMap = mainp.state.map;
    // cout << "opn2: ";
    // for (Point2D box : opn2.box)
    // {
    //     tmpMap[box.y][box.x] = true;
    //     cout << box.x << ' ' << box.y << " | ";
    // }
    // cout << endl;
    // solver.operation.flood(tmpMap, opn2.mask, mainp.state.man);

    // cout << "is Connected :" << solver.operation.isConnected(opn1, opn2) << endl;

    // return 0;
    // //////////////////

    attachText = "This is a program runing automatically";
    showState(mainp.state, attachText, 0, 0);

    cout << "start to get answer!" << endl;
    pathText = solver.solve(mainp.state);

    if (pathText == "Error")
    {
        cout << "Cannot find an answer!" << endl;
        return 0;
    }
    cout << "I have get the answer! totally " << pathText.size() << "1 step!" << endl;

    cin >> movement;

    for (int i = 0; i < pathText.size(); i++)
    {
        mainp.MoveToward(pathText[i]);
        showState(mainp.state, attachText, 1, 0);
        usleep(100000);
    }
    cout << "Program end!" << endl;
    return 0;
}